package com.itcast.ssm.controller;


import com.itcast.ssm.common.CommonResult;
import com.itcast.ssm.common.CommonResultCode;
import com.itcast.ssm.pojo.Order;
import com.itcast.ssm.response.OwnerOrder;
import com.itcast.ssm.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 三人行
 * @since 2020-09-12
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @GetMapping(value = "findAll")
    public CommonResult findAll(@RequestParam(value = "owner_id") Integer ownerId,
                        @RequestParam(value = "order_status") Integer orderStatus) {
        List<OwnerOrder> orders = orderService.findAll(ownerId, orderStatus);
        return new CommonResult(CommonResultCode.CommonSuccess, orders);
    }

    @GetMapping(value = "rent_out_message")
    public CommonResult rentOutMessage(@RequestParam(value = "owner_id") Integer ownerId,
                                       @RequestParam(value = "order_status") Integer orderStatus) {
        List<OwnerOrder> orders = orderService.findAll(ownerId, orderStatus);
        return new CommonResult(CommonResultCode.CommonSuccess, orders);
    }

    @DeleteMapping(value = "")
    public CommonResult delete(@RequestParam(value = "order_id") Integer orderId) {
        Integer result = orderService.delete(orderId);
        if(result <= 0) {
            return CommonResult.CommonFail();
        }
        return CommonResult.CommonSuccess();
    }

    @PutMapping(value = "")
    public CommonResult rentOut(@RequestParam(value = "order_id") Integer orderId) {
        Integer result = orderService.rentOut(orderId);
        if(result <= 0) {
            return CommonResult.CommonFail();
        }
        return CommonResult.CommonSuccess();
    }
}

